package hichecker.ui.general_views.container_panel;

import hichecker.controller.MainController;
import hichecker.tools.event.HiCheckerEvent;
import hichecker.tools.event.abstract_event.Observable;
import hichecker.tools.event.abstract_event.Observer;
import hichecker.tools.export.CSVFile;
import hichecker.ui.general_views.container_panel.content.ContentPanel;
import hichecker.ui.general_views.container_panel.navigation.NavigationPanel;
import hichecker.university.UniversityPart;

import java.awt.BorderLayout;
import java.awt.Color;
import java.util.List;

import javax.swing.BorderFactory;
import javax.swing.JPanel;
import javax.swing.JSplitPane;
import javax.swing.border.Border;

public class ContainerPanel extends ContentPanel implements Observer<HiCheckerEvent> {
	private MainController controller;
	private JPanel contentPanel;
	private ContentPanel content;
	private NavigationPanel menu;

	public ContainerPanel(UniversityPart item, final MainController controller) {
		super(item);

		this.controller = controller;

		// Observer
		controller.addObserver(this);

		// Content
		contentPanel = new JPanel();
		contentPanel.setLayout(new BorderLayout());

		// Menu
		menu = new NavigationPanel(item, this, controller);

		// Border
		setConfigBorder();

		// Layout
		JSplitPane splitPane = new JSplitPane();
		splitPane.setLeftComponent(menu);
		splitPane.setRightComponent(contentPanel);
		splitPane.setResizeWeight(0);

		setLayout(new BorderLayout());
		add(splitPane, BorderLayout.CENTER);
	}

	@Override
	public ContentPanel switchViewSettings() {
		return null;
	}

	public ContentPanel getContent() {
		return content;
	}

	public void setContent(ContentPanel newContent) {
		if (content != null && content instanceof ContainerPanel) {
			controller.removeObserver((ContainerPanel) content);
			controller.removeObserver(((ContainerPanel) content).getMenu());
		}

		content = newContent;

		contentPanel.removeAll();
		contentPanel.add(content, BorderLayout.CENTER);
		validate();

		if (!(content instanceof ContainerPanel)) {
			controller.setViewedElement(content.getItem());
		}
		setConfigBorder();
	}

	@Override
	public void notify(Observable<? extends HiCheckerEvent> sender, HiCheckerEvent event) {
		switch (event.getEventType()) {
		case SWITCH:
			// Switch view
			if (content != null && !(content instanceof ContainerPanel))
				setContent(content.switchViewSettings());
			setConfigBorder();
			break;
		}
	}

	private void setConfigBorder() {
		boolean showConfig = controller.isShowConfig() && content != null
		&& !(content instanceof ContainerPanel);

		Border border = showConfig ? BorderFactory.createLineBorder(Color.RED) : BorderFactory
				.createEmptyBorder();
		setBorder(border);
	}

	public NavigationPanel getMenu() {
		return menu;
	}

	@Override
	public boolean isCorrect() {
		return content.isCorrect();
	}

	@Override
	public List<CSVFile> toCSV(String topLevel) {
		return content.toCSV(topLevel);
	}
}
